//
//  MSDetailInfoCell.m
//  SocietySale
//
//  Created by Mike on 16/11/10.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSDetailInfoCell.h"

@interface MSDetailInfoCell ()
@property (nonatomic, strong) UIButton            *moreBtn;
@property (nonatomic, assign) BOOL                isClick;
@end

@implementation MSDetailInfoCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.keyLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 10, 60, 20)];
        self.keyLabel.font = HeitiFont(14);
        self.keyLabel.textColor = kDarkGrayColor;
//        self.keyLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:self.keyLabel];
        
        self.valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.keyLabel.right+10, 10, SCREEN_WIDTH-90, 20)];
        self.valueLabel.font = HeitiFont(14);
        self.valueLabel.numberOfLines = 0;
        self.valueLabel.textColor = kLightBlackColor;
        [self.contentView addSubview:self.valueLabel];
        
        _horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(12, 49.4, SCREEN_WIDTH-24, 0.6)];
        _horizonLineView.backgroundColor = kNormalGray;
        [self.contentView addSubview:_horizonLineView];
        
        NSMutableAttributedString *btnAttstr = [[NSMutableAttributedString alloc] initWithString:@"…" attributes:@{NSForegroundColorAttributeName:kDarkGrayColor}];
        NSAttributedString *tracStr = [[NSAttributedString alloc] initWithString:@"[更多]" attributes:@{NSForegroundColorAttributeName:kBlueColor}];
        [btnAttstr appendAttributedString:tracStr];
        
        _moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _moreBtn.frame = CGRectMake(SCREEN_WIDTH-68, 50, 57, 20);
        _moreBtn.titleLabel.font = HeitiFont(13);
        _moreBtn.titleLabel.textAlignment = NSTextAlignmentRight;
        _moreBtn.backgroundColor = [UIColor whiteColor];
        [_moreBtn addTarget:self action:@selector(moreClick:) forControlEvents:UIControlEventTouchUpInside];
        [_moreBtn setAttributedTitle:btnAttstr forState:UIControlStateNormal];
        [self.contentView addSubview:_moreBtn];

        _isClick = NO;
    }
    return self;
}

- (void)setData:(DOKeyValue *)value
{
    _data = [value copy];
    self.keyLabel.text = _data.key;
    self.valueLabel.text = _data.value;
    CGSize size = [HDUtil sizeWithString:_data.value font:[UIFont systemFontOfSize:14] size:CGSizeMake(SCREEN_WIDTH-90, CGFLOAT_MAX)];
    self.valueLabel.height = size.height>20?size.height+8:20;//     3行  size.height-8
        
    if (size.height<20) {//   1行
        self.valueLabel.height = 20;
    }
    if (size.height>30 && size.height<40) {//  两行  33
        self.valueLabel.height = size.height+8;
    }
    if (size.height>40 && size.height<60) {//   3行  50
        self.valueLabel.height = size.height+8;
    }

    if (size.height>60) { //   大于3行
        _moreBtn.hidden = NO;
        if (_isClick) {//点击过
            if (size.height>350) {
                self.valueLabel.height = size.height+50;
                _moreBtn.top = size.height+40;
            }
            else{
                self.valueLabel.height = size.height+18;
                _moreBtn.top = size.height+8;
            }
            
            NSMutableAttributedString *btnAttstr = [[NSMutableAttributedString alloc] initWithString:@"" attributes:@{NSForegroundColorAttributeName:kDarkGrayColor}];
            NSAttributedString *tracStr = [[NSAttributedString alloc] initWithString:@"[收起]" attributes:@{NSForegroundColorAttributeName:kBlueColor}];
            [btnAttstr appendAttributedString:tracStr];
            [_moreBtn setAttributedTitle:btnAttstr forState:UIControlStateNormal];
        }
        else{//没点击过
            self.valueLabel.height = 68-8;
            _moreBtn.top = 50;
            
            NSMutableAttributedString *btnAttstr = [[NSMutableAttributedString alloc] initWithString:@"…" attributes:@{NSForegroundColorAttributeName:kDarkGrayColor}];
            NSAttributedString *tracStr = [[NSAttributedString alloc] initWithString:@"[更多]" attributes:@{NSForegroundColorAttributeName:kBlueColor}];
            [btnAttstr appendAttributedString:tracStr];
            [_moreBtn setAttributedTitle:btnAttstr forState:UIControlStateNormal];
        }
    }
    else{
        _moreBtn.hidden = YES;
    }
    
    if ([_data.key containsString:@"电话"]) {
        self.valueLabel.textColor = kBlueColor;
    }
    else{
        self.valueLabel.textColor = kLightBlackColor;
    }
}

- (void)moreClick:(UIButton*)btn{
    btn.selected = !btn.selected;
    _isClick = btn.selected;
    if ([self.delegate respondsToSelector:@selector(clickMoreBtnIsFold:)]) {
        [self.delegate clickMoreBtnIsFold:btn.selected];
    }
}

+(CGFloat)cellHeight:(DOKeyValue *)data{
    CGSize size = [HDUtil sizeWithString:data.value font:[UIFont systemFontOfSize:14] size:CGSizeMake(SCREEN_WIDTH-90, CGFLOAT_MAX)];
    if (size.height<20) {//   1行    17   
        return 14+26;
    }
    if (size.height>30 && size.height<40) {//  两行  33
        return size.height+26;
    }
    if (size.height>40 && size.height<60) {//  3行  50.05
        return 68+16;
    }
    if (size.height>60) {// 大于3行  68.05
        if (size.height>350){
            return size.height+66;
        }
        else{
            return size.height+36;
        }
        
    }
    return 40;
}

@end
